<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="/admin/include :: head">
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
<!--<div ></div>-->
<section class="ftco-section">
    <div id="vueController" style="margin-left: 20px">
        <h2>轮播图管理</h2>
        <el-divider></el-divider>
        <el-button type="primary" @click="handleAddDialog()">新增</el-button>
        <el-table :data.sync="dataList" border style="margin-top: 20px">
<!--            <el-table-column prop="order" label="顺序" width="180">-->
<!--            </el-table-column>-->
            <el-table-column prop="imgUrl" label="文件路径" width="180">
            </el-table-column>
            <el-table-column label="预览" width="180">
                <template slot-scope="scope">
                    <img :src="scope.row.imgUrl" alt="" height="50px">
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
<!--                    <el-button-->
<!--                            size="mini"-->
<!--                            @click="handleEditDialog(scope.$index, scope.row)">编辑-->
<!--                    </el-button>-->
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="新增轮播图" :visible.sync="dialogFormVisible">
            <el-form :model="form">
<!--                <el-form-item label="顺序">-->
<!--                    <el-input v-model="form.order" autocomplete="off"></el-input>-->
<!--                </el-form-item>-->
                <el-upload
                        action="/common/upload"
                        ref="upload"
                        drag
                        :limit="1"
                        :file-list="fileList"
                        :on-success="handleUploadFinish"
                        list-type="picture">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">提交</el-button>
            </div>
        </el-dialog>
    </div>
</section>

</body>

<script type="module">
    import {getCarouselList,addCarousel,delCarouselList} from "/static/js/api/carousel.js";

    new Vue({
        el: '#vueController',
        data() {
            return {
                dataList: [],
                fileList: [],
                dialogFormVisible: false,
                form: {},
                formTemplate: {
                    id: "",
                    order: undefined,
                    imgUrl: "",
                },
            }
        },
        created() {
            this.getList();
        },
        methods: {
            getList() {
                getCarouselList().then(res => {
                    console.log(res)
                    this.dataList = res.data;
                })
            },
            submitUpload() {
                this.$refs.upload.submit();
            },
            handleUploadFinish(response, file, fileList){
                console.log(response)
                this.form.imgUrl = response.data;
            },
            handleAddDialog() {
                this.form = Object.assign({}, this.formTemplate);
                this.dialogFormVisible = true;
            },
            handleEditDialog(index, row) {
                this.form = Object.assign({}, row);
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                delCarouselList(row.id).then(res=>{
                    console.log("删除成功");
                    this.getList();
                })
            },
            handleSubmit() {
                addCarousel(this.form).then(res=>{
                    console.log("添加成功");
                    this.getList();
                })
                this.fileList=[];
                this.dialogFormVisible = false;

            },
        }

    })
</script>
</html>